package com.doupi.partner.op.controller.configure;

import com.doupi.partner.dal.model.SystemConfig;
import com.doupi.partner.service.SystemConfigService;
import com.doupi.partner.util.AjaxResponse;
import com.doupi.partner.util.DataTableData;
import com.doupi.partner.util.Pagination;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("configure/systemConfig")
public class SystemConfigController {
	private final static String TEMPLATE_FOLDER = "configure/";

	private final SystemConfigService systemConfigService;

	@Autowired
	public SystemConfigController(SystemConfigService systemConfigService) {
		this.systemConfigService = systemConfigService;
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index() {
		return TEMPLATE_FOLDER + "systemConfig/index";
	}

	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public DataTableData<SystemConfig> list(Integer offset, Integer limit, SystemConfig systemConfig) {
		Pagination<SystemConfig, SystemConfig> pagination = new Pagination<>(offset, limit, systemConfig);
		pagination = systemConfigService.querySystemConfigs(pagination);
		return new DataTableData<>(pagination.getTotalCount(), pagination.getRecords());
	}

	@ResponseBody
	@RequestMapping(value = "list/{parentKey}", method = RequestMethod.POST)
	public AjaxResponse list(@PathVariable("parentKey") String parentKey) {
		AjaxResponse ajaxResponse = AjaxResponse.defaultResponse();
		SystemConfig systemConfig = new SystemConfig();
		systemConfig.setParentKey(parentKey);
		Pagination<SystemConfig, SystemConfig> pagination = new Pagination<>(0, Integer.MAX_VALUE, systemConfig);
		pagination = systemConfigService.querySystemConfigs(pagination);
		ajaxResponse.setData(pagination.getRecords());
		return ajaxResponse;
	}

	@ResponseBody
	@RequestMapping(value = "delete/{key}", method = RequestMethod.DELETE)
	public AjaxResponse delete(@PathVariable("key") String key) {
		systemConfigService.deleteByPrimaryKey(key);
		return AjaxResponse.defaultResponse();
	}

	@RequestMapping(value = "edit/{key}", method = RequestMethod.GET)
	public String edit(ModelMap modelMap, @PathVariable(value = "key") String key) {
		SystemConfig systemConfig = systemConfigService.selectByPrimaryKey(key);
		modelMap.put("systemConfig", systemConfig);
		return TEMPLATE_FOLDER + "systemConfig/edit";
	}

	@RequestMapping(value = "addSubordinate/{parentKey}", method = RequestMethod.GET)
	public String addSubordinate(ModelMap modelMap, @PathVariable(value = "parentKey") String parentKey) {
		SystemConfig systemConfig = new SystemConfig();
		SystemConfig systemConfigParent = systemConfigService.selectByPrimaryKey(parentKey);
		systemConfig.setParentKey(parentKey);
		if (null != systemConfigParent) {
			systemConfig.setKeyPath(systemConfigParent.getKeyPath() + "/" + parentKey);
		} else {
			systemConfig.setKeyPath("/" + parentKey);
		}
		systemConfig.setParentKey(parentKey);
		modelMap.put("systemConfig", systemConfig);
		return TEMPLATE_FOLDER + "systemConfig/edit";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(SystemConfig systemConfig) {
		SystemConfig systemConfigByKey = systemConfigService.selectByPrimaryKey(systemConfig.getKey());

		if (StringUtils.isNotBlank(systemConfig.getParentKey()) && !StringUtils.equals("0", systemConfig.getParentKey())) {
			SystemConfig systemConfigParent = systemConfigService.selectByPrimaryKey(systemConfig.getParentKey());
			if (null != systemConfigParent) {
				systemConfig.setKeyPath(systemConfigParent.getKeyPath() + "/" + systemConfigParent.getKey());
			} else {
				systemConfig.setKeyPath("/0");
				systemConfig.setParentKey("0");
			}
		} else {
			systemConfig.setKeyPath("/0");
			systemConfig.setParentKey("0");
		}

		if (null != systemConfigByKey) {
			systemConfigByKey.setValue(systemConfig.getValue());
			systemConfigByKey.setName(systemConfig.getName());
			systemConfigByKey.setMemo(systemConfig.getMemo());
			systemConfigService.updateByPrimaryKeySelective(systemConfigByKey);
		} else {
			systemConfigService.insert(systemConfig);
		}
		return "redirect:index";
	}
}
